package com.easepal7506a.project.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.easepal.socketiolib.model.SocketModel;
import com.easepal7506a.project.Constant.CommmandNum;
import com.easepal7506a.project.R;
import com.easepal7506a.project.manager.DataManager;
import com.easepal7506a.project.network.CommondManager;
import com.easepal7506a.project.observer.SocketDataObserver;
import com.easepal7506a.project.utils.ActivityUtils;
import com.example.reslib.utils.ToastUtils;
import com.ogawa.base.callback.NormalCallback;
import com.ogawa.base.network.BaseResponse;
import com.ogawa.base.utils.CommonUtil;

import java.lang.ref.WeakReference;

/**
 * Created by FHB on 2018/4/9.
 */
public class LayoutFunctionView7506 extends LinearLayout implements View.OnClickListener,SocketDataObserver.SocketReceive {
    private Context mContext;
    private ImageView mIvAdjust;
    private ImageView mIvFoodRoll;
    private ImageView mIvHot;
    private ImageView mIvLed;
    private LinearLayout mLlAdjust;
    private LinearLayout mLlFoodRoll;
    private LinearLayout mLlMusic;
    private LinearLayout mLlHoot;
    private LinearLayout mLlLed;
    private MyHandler myHandler;

    public LayoutFunctionView7506(Context context) {
        super(context);
        mContext = context;
    }

    public LayoutFunctionView7506(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        myHandler = new MyHandler(this);
        mIvAdjust = findViewById(R.id.layout_function_tv_zttj);
        mIvFoodRoll = findViewById(R.id.layout_function_tv_jdgl);
        mIvHot = findViewById(R.id.layout_function_tv_ybwg);
        mIvLed = findViewById(R.id.layout_function_tv_led);

        mIvAdjust.setOnClickListener(this);
        mIvFoodRoll.setOnClickListener(this);
        mIvHot.setOnClickListener(this);
        mIvLed.setOnClickListener(this);
        mIvHot.setSelected(DataManager.getInst().getValue(CommmandNum.mStatusHot).equals("1"));
        mIvFoodRoll.setSelected(DataManager.getInst().getValue(CommmandNum.mStatusJdgl).equals("1"));
        mIvLed.setSelected(DataManager.getInst().getValue(CommmandNum.mStatusLed).equals("1"));
        SocketDataObserver.getInst().registerObserver(this);
    }

    @Override
    public void onRceiveSocket(SocketModel model) {
        if(model.getKey().equals(CommmandNum.mStatusHot)){
            mIvHot.setSelected(DataManager.getInst().getValue(CommmandNum.mStatusHot).equals("1"));
        }
       else if(model.getKey().equals(CommmandNum.mStatusJdgl)){
            mIvFoodRoll.setSelected(DataManager.getInst().getValue(CommmandNum.mStatusJdgl).equals("1"));
        }
        else if(model.getKey().equals(CommmandNum.mStatusLed)){
            mIvLed.setSelected(DataManager.getInst().getValue(CommmandNum.mStatusLed).equals("1"));
        }

    }
    public void onDestory(){
        SocketDataObserver.getInst().removeObserver(this);
    }


    private  class MyHandler extends Handler {
        WeakReference<LayoutFunctionView7506> mLayoutFunctionViewWeakReference;

        MyHandler(LayoutFunctionView7506 mLayoutFunctionView7506) {
            mLayoutFunctionViewWeakReference = new WeakReference<>(mLayoutFunctionView7506);
        }

        @Override
        public void handleMessage(Message msg) {
            final LayoutFunctionView7506 mLayoutFunctionView7506 = mLayoutFunctionViewWeakReference.get();
            if (mLayoutFunctionView7506 != null) {
                String data  = (String) msg.obj;



            }
        }
    }
    private void sendCommond(String code){
        CommondManager.getInst().sendShortCommond(code, new NormalCallback() {
            @Override
            public void onSuccess(BaseResponse<String> program) {

            }

            @Override
            public void onFailure(String error) {
                ToastUtils.showLongToast(mContext,error+"");
            }
        });
    }
    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.layout_function_tv_zttj) {
            ActivityUtils.startAttitudeAdjustmentActivity(mContext);

        } else if (i == R.id.layout_function_tv_jdgl) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.JIAODGL);

            }


        }  else if (i == R.id.layout_function_tv_ybwg) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.HOT);
            }


        } else if (i == R.id.layout_function_tv_led) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                sendCommond(CommmandNum.LED);
            }


        }

    }
}
